package com.dd.census.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dd.census.emums.DictionaryGroupEnum;
import com.dd.census.entity.Dictionary;
import com.dd.census.service.DictionaryService;
import com.dd.census.tools.TokenHelper;
import com.dd.census.tools.resopnse.ResultResponse;
import com.dd.census.tools.resopnse.emums.ResultStatus;

@RestController
@RequestMapping("census/dictionary")
public class DictionaryController {

	private static final Logger logger = LoggerFactory.getLogger(DictionaryController.class);

	@Autowired
	private DictionaryService dictionaryService;

	@RequestMapping("selectPage")
	public ResultResponse<Object> select(@RequestBody Dictionary dictionary, HttpServletRequest request) {
		try {
			return ResultResponse.buildSuccess(dictionaryService.selectPage(dictionary));

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("selectAll")
	public ResultResponse<Object> selectAll(@RequestBody Dictionary dictionary, HttpServletRequest request) {
		try {
			return ResultResponse.buildSuccess(dictionaryService.selectAll(dictionary));

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("/{id}")
	public ResultResponse<Object> getById(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {
			return ResultResponse.buildSuccess(dictionaryService.selectById(id));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("selectGroup")
	public ResultResponse<Object> selectGroup(@RequestBody Dictionary dictionary, HttpServletRequest request) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("items", DictionaryGroupEnum.toList());

			return ResultResponse.buildSuccess(map);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("insert")
	public ResultResponse<Object> insert(HttpServletRequest request, @RequestBody Dictionary dictionary) {

		try {
			dictionary.setCreateBy(TokenHelper.getUserId(request));
			dictionary.setCreateTime(new Date());
			// 插入数据
			List<Dictionary> list = dictionaryService.selectCode(dictionary.getGroupName());
			int next = 0;
			for(int i = 0; i<list.size(); i++){
				if(dictionary.getCode().equals(list.get(i).getCode())){
					next+=i;
				}
			}
			
			if(next == 0){
		    	dictionaryService.insert(dictionary);
	        }else{
	    	   
	        }

			return ResultResponse.buildSuccess(next);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("update")
	public ResultResponse<Object> update(HttpServletRequest request, @RequestBody Dictionary dictionary) {

		try {
			dictionary.setUpdateBy(TokenHelper.getUserId(request));
			dictionary.setUpdateTime(new Date());

			return ResultResponse.buildSuccess(dictionaryService.update(dictionary));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}
	
	@RequestMapping("delete")
	public ResultResponse<Object> delete(HttpServletRequest request, @RequestBody Dictionary dictionary) {

		try {
			return ResultResponse.buildSuccess(dictionaryService.delete(dictionary));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}
	
	@RequestMapping("getDictionarys")
	public ResultResponse<Object> getDictionary(HttpServletRequest request,String groupName) {
		
        try {
        	Dictionary dictionary = new Dictionary();
    		dictionary.setGroupName(groupName);
            List<Dictionary> list =dictionaryService.selectAll(dictionary);

			return ResultResponse.buildSuccess(list);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
    }

}
